import { getDataPlnel, getTable, getDetail, getAddress, getAdvise, stop, exportUser } from '@/api/userManage'
import { Message } from 'element-ui'
// import { toBlobUrlByUser } from '@/utils/tools/downloadImg'
import download from '@/utils/tools/downloadImg'

const oldState = { // 用于重置state数据
  tableData: [

  ],
  panelData: [
    { id: 0, number: 123456, type: 'today_b', desc: '今日新增' },
    { id: 1, number: 123456, type: 'mouthAdd_o', desc: '本月新增' },
    { id: 2, number: 123456, type: 'user', desc: '累计用户' }

  ],
  params: {
    page: 0,
    size: 10,
    userName: null,
    userPhone: null

  },
  totalNum: {
    total: 0
  }

}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_tableData (state, data) {
    state.tableData = data
  },
  set_panelData (state, data) {
    state.panelData[0].number = data.todayCount
    state.panelData[1].number = data.monthCount
    state.panelData[2].number = data.total
  },
  set_page (state, data) {
    state.params.page = data - 1
  },
  set_size (state, data) {
    state.params.size = data
  },
  set_userName (state, data) {
    state.params.userName = data
  },
  set_userPhone (state, data) {
    state.params.userPhone = data
  },
  set_total (state, data) {
    state.totalNum.total = data
  }
}
const actions = {
  // 获取看板数据
  getDataPlnelData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDataPlnel(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_panelData', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getTableData ({ state, commit }) {
    return new Promise((resolve, reject) => {
      getTable(state.params).then(async res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          for (let i = 0; i < res.data.data.userList.length; i++) {
            if (res.data.data.userList[i].userImg !== null) {
              res.data.data.userList[i].imgSrc = await download.toBlobUrlByUser(res.data.data.userList[i].userImg)
            } else {
              res.data.data.userList[i].imgSrc = null
            }
          }
          //  res.data.data.userList.forEach(async item => {
          //   if (item.userImg !== null) {
          //     item.imgSrc = await download.toBlobUrlByUser(item.userImg)
          //   } else {
          //     item.imgSrc = null
          //   }
          // })
          // console.log(res.data.data.userList)
          commit('set_tableData', res.data.data.userList)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getDetailData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      getDetail(playload).then(async res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          if (res.data.data.userExportVo.userImg) {
            res.data.data.userExportVo.imgSrc = await download.toBlobUrlByUser(res.data.data.userExportVo.userImg)
          } else {
            res.data.data.userExportVo.imgSrc = null
          }
          commit('set_tableData', res.data.data)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getAddressData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      getAddress(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getAdviseData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      getAdvise(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  stopUser ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      stop(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 导出用户表
  exportUserData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      exportUser(playload).then(res => {
        // console.log(res)
        // console.log(1)
        // resolve(res.data)
        // console.log(res)
        // if (res.data.code === 200 && res.data) {

        // } else {
        //   reject(res.data.message)
        // }
        if (res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            const newRes = JSON.parse(reader.result)
            // console.log(newRes)
            Message({
              type: 'error',
              message: newRes.message,
              duration: 1000
            })
          }
        } else {
          // 正确回调函数
          resolve(res.data)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
